﻿using LayeredSkin.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Top_Meeting.UI;

namespace Top_Meeting.Window
{
    public partial class Frm_Base : LayeredForm
    {
        protected string title = "";
        protected bool canResize = false;
        protected Color bgColor = Color.White;
        protected Color fgColor = Color.Black;
        protected Padding prePadding = Padding.Empty;

        private bool _EnableAnimation = false;


        public Frm_Base()
        {
            InitializeComponent();

            BaseInit();
        }


        #region OVERRIDE_METHOD

        protected override void OnLoad(EventArgs e)
        {
            layPanel_Title.BringToFront();

            base.OnLoad(e);
        }

        #endregion


        #region PRIVATE_METHOD

        private void BaseInit()
        {
            EnableAnimation = _EnableAnimation;
        }

        private void SetFont(Font _font)
        {
            Helper.SetFont(this, _font);
        }

        #endregion


        #region PROTECTED_METHOD

        protected void SetWindowState(FormWindowState _windowState)
        {
            WindowState = _windowState;
        }

        protected void SetWindowStateMini()
        {
            SetWindowState(FormWindowState.Minimized);
        }

        protected void SetWindowStateNormal()
        {
            SetWindowState(FormWindowState.Normal);
        }

        protected void SetWindowStateMax()
        {
            this.MaximumSize = new Size(Screen.GetWorkingArea(this).Width, Screen.GetWorkingArea(this).Height);
            SetWindowState(FormWindowState.Maximized);
        }

        protected void SetWindowStateSwitch()
        {
            switch (WindowState)
            {
                case FormWindowState.Normal:
                    SetWindowStateMax();
                    break;
                case FormWindowState.Minimized:
                    break;
                case FormWindowState.Maximized:
                    SetWindowStateNormal();
                    break;
                default:
                    break;
            }
        }

        protected void SetControlBoxButton(bool mbtn, bool sbtn, bool cbtn)
        {
            layBtn_Mini.Visible = mbtn;
            layBtn_StateSwitch.Visible = sbtn;
            layBtn_Close.Visible = cbtn;

            layPanel_Title.Left = layPanel_Title.Parent.Width - layPanel_Title.Width - layPanel_Title.Parent.Padding.Right;
            layPanel_Title.Top = layPanel_Title.Parent.Padding.Top;
        }

        protected void ControBoxActive(bool active)
        {
            layPanel_ControlBox.Visible = active;
        }

        protected void WindowClose()
        {
            Close();
        }

        #endregion


        #region PUBLIC_METHOD

        public void Frm_MouseDown(object sender, MouseEventArgs e)
        {
            this.OnMouseDown(e);
        }

        #endregion


        #region VIRTUAL

        protected virtual void FrmInit()
        {
            UpdateLayout();
            InitEvent();
        }

        protected virtual void UpdateLayout()
        {
            this.Text = title;
            this.ForeColor = fgColor;
            this.BackColor = bgColor;

            //Font
            //SetFont(TMFont.GetFont("微软雅黑"));

            //Form
            prePadding = Padding;
            this.Size = this.BackgroundImage != null ? this.BackgroundImage.Size : this.Size;
            layPanel_Body.SendToBack();

            //this.CenterToParent();
        }

        protected virtual void InitEvent()
        {
            //FORM
            this.SizeChanged += Frm_Base_SizeChanged;
            layPanel_Body.MouseDown += Frm_MouseDown;
            layPanel_Title.MouseDown += Frm_MouseDown;
            layBtn_Mini.Click += LayBtn_Mini_Click;
            layBtn_StateSwitch.Click += LayBtn_StateSwitch_Click;
            layBtn_Close.Click += LayBtn_Close_Click;
        }

        private void LayeredButton1_Click(object sender, EventArgs e)
        {
            Padding px0 = Padding.Empty;
            Padding px20 = new Padding(20);

            this.Padding = this.Padding == px0 ? px20 : px0;
        }

        #endregion


        #region CONTROL_EVENT

        //FORM
        private void Frm_Base_SizeChanged(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Maximized)
            {
                prePadding = Padding;
                Padding = Padding.Empty;
            }
            if (WindowState == FormWindowState.Normal)
            {
                Padding = prePadding;
            }
        }

        private void LayBtn_Close_Click(object sender, EventArgs e)
        {
            WindowClose();
        }

        private void LayBtn_StateSwitch_Click(object sender, EventArgs e)
        {
            SetWindowStateSwitch();
        }

        private void LayBtn_Mini_Click(object sender, EventArgs e)
        {
            SetWindowStateMini();
        }

        #endregion


        #region WIN_MESSAGE


        #region RESIZE

        //const int WM_NCHITTEST = 0x0084;
        //const int HTLEFT = 10;
        //const int HTRIGHT = 11;
        //const int HTTOP = 12;
        //const int HTTOPLEFT = 13;
        //const int HTTOPRIGHT = 14;
        //const int HTBOTTOM = 15;
        //const int HTBOTTOMLEFT = 0x10;
        //const int HTBOTTOMRIGHT = 17;
        //protected override void WndProc(ref Message m)
        //{
        //    try
        //    {
        //        base.WndProc(ref m);
        //        switch (m.Msg)
        //        {
        //            case WM_NCHITTEST:
        //                if (canResize)
        //                {
        //                    Point vPoint = new Point((int)m.LParam & 0xFFFF,
        //                        (int)m.LParam >> 16 & 0xFFFF);
        //                    vPoint = PointToClient(vPoint);
        //                    if (vPoint.X <= this.Padding.Left + 5 & vPoint.X >= this.Padding.Left - 5)
        //                        if (vPoint.Y <= this.Padding.Top + 5)
        //                            m.Result = (IntPtr)HTTOPLEFT;
        //                        else if (vPoint.Y >= ClientSize.Height - this.Padding.Bottom - 5)
        //                            m.Result = (IntPtr)HTBOTTOMLEFT;
        //                        else m.Result = (IntPtr)HTLEFT;
        //                    else if (vPoint.X >= ClientSize.Width - this.Padding.Right - 5 & vPoint.X <= ClientSize.Width - this.Padding.Right + 5)
        //                        if (vPoint.Y <= this.Padding.Top + 5)
        //                            m.Result = (IntPtr)HTTOPRIGHT;
        //                        else if (vPoint.Y >= ClientSize.Height - this.Padding.Bottom - 5)
        //                            m.Result = (IntPtr)HTBOTTOMRIGHT;
        //                        else m.Result = (IntPtr)HTRIGHT;
        //                    else if (vPoint.Y <= this.Padding.Top + 5 & vPoint.Y >= this.Padding.Top - 5)
        //                        m.Result = (IntPtr)HTTOP;
        //                    else if (vPoint.Y >= ClientSize.Height - this.Padding.Bottom - 5 & vPoint.Y <= ClientSize.Height - this.Padding.Bottom + 5)
        //                        m.Result = (IntPtr)HTBOTTOM;
        //                    break;
        //                }
        //                break;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //    }
        //}

        #endregion


        #endregion

    }
}
